﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using Newtonsoft.Json;
using QDT.Common;
using QDT.Core.Domain;
using QDT.Common.Data;
using QDT.Core.Members;
using QDT.MVC.Security;
using QDT.Payment;
using QDT.Services.Members;

namespace QDT.Web.Admin.Controllers
{
    [SecurityModule(Name = "提现申请管理")]
    public class UserCarryController : BaseController
    {
        [SecurityNode(Name = "申请列表")]
        public ActionResult Index(int? page, GridSortOptions sort, int? status, string username, DateTime? start, DateTime? end, decimal max = 0, decimal min = 0)
        {
            const int pageSize = 20;
            var pageNumber = page ?? 1;

            var filter = PredicateExtensionses.True<UserCarry>();

            if (!string.IsNullOrWhiteSpace(username))
            {
                var user = IoC.Resolve<IUserService>().GetUserByName(username);
                if (user != null)
                {
                    filter = filter.And(x => x.UserId == user.ID);
                }
            }
            if (status != null) filter = filter.And(x => x.Status == status);

            if (start.HasValue) filter = filter.And(x => x.CreateTime >= start.Value);

            if (end.HasValue) filter = filter.And(x => x.CreateTime <= end.Value);

            if (min > 0) filter = filter.And(x => x.Money >= min);

            if (max > 0 && max > min) filter = filter.And(x => x.Money <= max);

            ViewData["sort"] = sort;

            var source = DbProvider.D<UserCarry>()
                                   .Where(filter)
                                   .OrderBy(sort.Column, sort.Direction == SortDirection.Descending);

            var pages = source.ToPagedList(pageNumber - 1, pageSize);
            return View(pages);
        }


        [SecurityNode(Name = "批量处理")]
        public ActionResult AllProcess(string carryIds)
        {
            try
            {
                var ids = carryIds.Split(',');
                if (ids.Any(x=>x.Equals(""))) throw new Exception("请选择");
                var service = IoC.Resolve<IUserService>();
                foreach (var id in ids)
                {
                    service.CarryCompleted(Convert.ToInt32(id));
                }
                DbProvider.SaveChanges();
                return Json(new { Status = "true", Message = "操作成功" });
            }
            catch (Exception e)
            {
                return Json(new { Status = "false", Message = "操作失败 : " + e.Message });
            }
        }

        [SecurityNode(Name = "处理提现页")]
        public ActionResult Process(int id)
        {
            var carry = DbProvider.GetByID<UserCarry>(id);
            return PartialView(carry);
        }

        [HttpPost]
        [SecurityNode(Name = "处理提现操作")]
        public ActionResult PostProcess(int id, int status, string msg, string desc)
        {
            if (status==0)
            {
                return Success("操作成功");
            }
            
            switch (status)
            {
                case 6:
                    IoC.Resolve<IUserService>().CarryCompleted(id,msg,desc);
                    break;
                case 2:
                    IoC.Resolve<IUserService>().RefusalCarry(id,msg,desc);
                    break;
                case 1:
                    var carry = DbProvider.GetByID<UserCarry>(id);
                    carry.SetStatus(1);
                    carry.UpdateTime = DateTime.Now;
                    var user = DbProvider.GetByID<User>(carry.UserId);
                    var log = user.DeductLockMoney(UserTransLog.TradeType.CarrayLockMoneyDefray, carry.Money, "", "提现成功，冻结金扣除");
                    DbProvider.Add(log);
                    DbProvider.SaveChanges();
                    break;
            }

            return Success("操作成功");
        }

        public ActionResult CarryQuery()
        {
            return Success("操作成功");
        }
       [SecurityNode(Name = "公司提款页")]
        public ActionResult CompanyCarry()
        {
            return View();
        }
        [SecurityNode(Name = "公司提款")]
        public ActionResult PostCompanyCarry(decimal money)
        {
           IoC.Resolve<IUserService>().CompanyCarry(money);
            return Success("操作失败");
        }

    }
}
